home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / U-Z / VideoToolBox Folder / VideoToolboxSources / GetScreenDevice.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-06  |  4.9 KB  |  174 lines  |  [TEXT/KAHL]

  1. /* GetScreenDevice.c
  2. Copyright © 1989-1993 Denis G. Pelli
  3. HISTORY:
  4. 3/20/90        dgp    make compatible with MPW C.
  5. 3/22/90    dgp    changed GetDeviceSlot to use the AuxDCEHandle instead of deducing it
  6.             from the baseAddr of the PixMap. This is a cleaner way to do it.
  7. 4/9/90    dgp    eliminated #define for Mainscrn mispelling in Color.h
  8. 10/17/90 dgp Added AddressToScreenDevice() for compatibility with built-in video on
  9.             the Mac IIci, IIsi, and LC.
  10. 10/18/90 dgp Added LocalToGlobalRect() and GetWindowDevice().
  11. 8/24/91        dgp    Made compatible with THINK C 5.0.
  12. 2/1/92    dgp    Fixed bugs in GetWindowDevice() which resulted in returning garbage GDHandle.
  13. 3/3/92    dgp    In GetScreenDevice(), skip inactive screens.
  14. 8/20/92    dgp    expanded comments of GetDeviceSlot(), noting that it works even with
  15.             built-in video, e.g. on Mac IIci.
  16. 8/26/92    dgp    GetDeviceSlot() now returns -1 if none, since zero is a legal slot.
  17.             GetScreenDevice() first checks for 8-bit QuickDraw().
  18. 9/10/92    dgp    Actually implemented the 8/26 change instead of just changing the 
  19.             documentation. Oops.
  20. */
  21. #include "VideoToolbox.h"
  22.  
  23. short int GetDeviceSlot(GDHandle device)
  24. // Gets the "slot" for any screen device, even if it's built-in video, e.g. on Mac
  25. // IIci or Quadra. See 1992 Inside Mac "Processes" page 4-11. Returns -1 if none.
  26. // Zero is a legal slot for built-in video devices.
  27. {
  28.     AuxDCEHandle myAuxDCEHandle;
  29.  
  30.     if(device == NULL) return -1;
  31.     myAuxDCEHandle=(AuxDCEHandle) GetDCtlEntry((**device).gdRefNum);
  32.     return ((**myAuxDCEHandle).dCtlSlot);
  33. }
  34.  
  35. GDHandle GetScreenDevice(int n)
  36. // Returns a handle to the n-th screen, where the MainDevice is the zero-th screen.
  37. // Returns NULL if request can't be satisfied.
  38. {
  39.     GDHandle device;
  40.     int i,error;
  41.     long value;
  42.  
  43.     if(n<0)return NULL;
  44.     error=Gestalt(gestaltQuickdrawVersion,&value);
  45.     if(error || value<gestalt8BitQD)return NULL;    // need 8-bit quickdraw
  46.     if(n==0) return GetMainDevice();
  47.     device=GetDeviceList();
  48.     i=0;
  49.     while(device!=NULL){
  50.         if (TestDeviceAttribute(device,screenDevice)
  51.             && !TestDeviceAttribute(device,mainScreen)
  52.             && TestDeviceAttribute(device,screenActive)){
  53.                 i++;
  54.                 if(i==n)break;
  55.             }
  56.         device = GetNextDevice(device);
  57.     }
  58.     return device;
  59. }
  60.  
  61. int GetScreenIndex(GDHandle device)
  62. // Inverse of GetScreenDevice(). Returns -1 if request can't be satisfied.
  63. {
  64.     int i,error;
  65.     long value;
  66.  
  67.     error=Gestalt(gestaltQuickdrawVersion,&value);
  68.     if(error || value<gestalt8BitQD)return 0;
  69.     if(device==NULL)return -1;
  70.     for(i=0;i<16;i++)if(device==GetScreenDevice(i))return i;
  71.     return -1;
  72. }
  73.  
  74. GDHandle GetWindowDevice(WindowPtr window)
  75. // Returns GDHandle of screen with largest intersection with the window's content.
  76. {
  77.     Rect r,overlap;
  78.     GDHandle device,dominantDevice=NULL;
  79.     long area,greatestArea;
  80.     WindowPtr oldWindow;
  81.     int error;
  82.     long value;
  83.  
  84.     if(window==NULL)return NULL;
  85.     error=Gestalt(gestaltQuickdrawVersion,&value);
  86.     if(error || value<gestalt8BitQD)return NULL;    // need 8-bit quickdraw
  87.     r=window->portRect;
  88.     GetPort(&oldWindow);
  89.     SetPort(window);
  90.     LocalToGlobalRect(&r);
  91.     SetPort(oldWindow);
  92.     device=GetDeviceList();
  93.     greatestArea=0;
  94.     while(device!=NULL){
  95.         if(TestDeviceAttribute(device,screenDevice)
  96.             && TestDeviceAttribute(device,screenActive)){
  97.                 SectRect(&r,&(*device)->gdRect,&overlap);
  98.                 area=(long)(overlap.right-overlap.left)*(overlap.bottom-overlap.top);
  99.                 if(area>greatestArea){
  100.                     greatestArea=area;
  101.                     dominantDevice=device;
  102.                 }
  103.             }
  104.         device=GetNextDevice(device);
  105.     }
  106.     return dominantDevice;
  107. }
  108.  
  109. GDHandle AddressToScreenDevice(Ptr address)
  110. // Returns a handle to the screen device containing pixels at the given address.
  111. // Returns NULL if request can't be satisfied.
  112. // The algorithm rounds addresses down to multiples of 16 MB and checks for equality.
  113. {
  114.     GDHandle device;
  115.     long mask;
  116.     int error;
  117.     long value;
  118.  
  119.     error=Gestalt(gestaltQuickdrawVersion,&value);
  120.     if(error || value<gestalt8BitQD)return NULL;    // need 8-bit quickdraw
  121.     mask=~(0x1000000-1);
  122.     device=GetDeviceList();
  123.     while(device!=NULL) {
  124.         if (TestDeviceAttribute(device,screenDevice) &&
  125.             (((long)address&mask)==((long)(**(**device).gdPMap).baseAddr&mask)))
  126.                 break;
  127.         device=GetNextDevice(device);
  128.     }
  129.     return device;
  130. }
  131.  
  132. GDHandle SlotToScreenDevice(int n)
  133. // Returns a handle to the screen device in slot n.
  134. // Returns NULL if request can't be satisfied.
  135. {
  136.     GDHandle device;
  137.  
  138.     device=GetDeviceList();
  139.     while(device!=NULL) {
  140.         if (TestDeviceAttribute(device,screenDevice) &&
  141.             GetDeviceSlot(device)==n)
  142.                 break;
  143.         device=GetNextDevice(device);
  144.     }
  145.     return device;
  146. }
  147.  
  148. short int AddressToSlot(Ptr address)
  149. // Old routine. Doesn't work with built in video: Mac IIci, IIsi, LC.
  150. // Use AddressToScreenDevice() instead.
  151. // Returns the slot a given address is in. Returns zero if none.
  152. {
  153.     unsigned long base;
  154.     base=(unsigned long) address;
  155.     if (0xf9000000 < base && base < 0xfeffffff)
  156.         return (base/0x1000000)%16;
  157.     return 0;
  158. }
  159.  
  160. void LocalToGlobalRect(Rect *r)
  161. {
  162.     Point pt={0,0};
  163.     
  164.     LocalToGlobal(&pt);
  165.     OffsetRect(r,pt.h,pt.v);
  166. }
  167.  
  168. void GlobalToLocalRect(Rect *r)
  169. {
  170.     Point pt={0,0};
  171.     
  172.     GlobalToLocal(&pt);
  173.     OffsetRect(r,pt.h,pt.v);
  174. }